import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:jtsell_app/pages/detail/goods_detail_page.dart';
import 'package:jtsell_app/pages/login/login_page.dart';
import 'package:jtsell_app/pages/login/register_page.dart';

class Routes {
  /// 首页地址
  final String _indexPage = '/';

  /// 页面标识常量
  final String _goodsDetailPage = '/goods_detail_page';
  final String _loginPage = "/login_page";
  final String _registerPage = "/register_page";

  ///路由对象
  Router _router;
  static Routes _instance;

  ///
  /// 单例模式
  ///
  static Routes getInstance() {
    if (_instance == null) {
      _instance = new Routes();
      _instance._router = Router();
      _instance._configureRoutes();
    }
    return _instance;
  }

  ///
  /// 路由对象的getter
  ///
  Router get router => _router;

  ///
  /// 初始化配置
  ///
  void _configureRoutes() {
    ///商品详情页
    _router.define(_goodsDetailPage,
        handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      String goodsID = params["goodsID"].first;
      return GoodsDetailPage(goodsID: goodsID);
    }));

    ///用户登录页面
    _router.define(_loginPage,
        handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      return LoginPage();
    }));

    ///用户注册页面
    _router.define(_registerPage,
        handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      return RegisterPage();
    }));
  }

  ///
  /// 出栈
  ///
  void pop(BuildContext context) {
    _router.pop(context);
  }

  ///
  /// 跳转到详情页
  ///
  void pushGoodsDetailPage(BuildContext context, String goodsID) {
    _router.navigateTo(context, "$_goodsDetailPage?goodsID=$goodsID", //跳转路径
        transition: TransitionType.fadeIn);
  }

  ///
  /// 跳转到登录页面
  ///
  Future loginPage(BuildContext context) {
    return _router.navigateTo(context, _loginPage, //跳转路径
        transition: TransitionType.material);
  }

  ///
  /// 跳转到登录页面
  ///
  Future registerPage(BuildContext context) {
    return _router.navigateTo(context, _registerPage, //跳转路径
        transition: TransitionType.material);
  }
}
